package com.lcf.app.quartz.demo;

import com.lcf.app.quartz.common.QuartzUtil;
import com.lcf.app.quartz.entity.JobInfo;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class Demo {

    @Resource
    private QuartzUtil quartzUtil;

    @PostConstruct
    public void demo() throws SchedulerException {
        //quartzUtil.clear();
        JobInfo job1 = new JobInfo()
                .setJobName("learn")
                .setTriggerName("learn")
                .setStatus(true)
                .setCron("0/5 * * * * ?")
                .setName("学习")
                .setJobData("学习...");
        JobInfo job2 = new JobInfo()
                .setJobName("eat")
                .setTriggerName("eat")
                .setStatus(true)
                .setCron("0/5 * * * * ?")
                .setName("吃饭")
                .setJobData("吃饭...");
        JobInfo job3 = new JobInfo()
                .setJobName("run")
                .setTriggerName("run")
                .setStatus(false)
                .setCron("0/5 * * * * ?")
                .setName("跑步")
                .setJobData("跑步...");
        //开始执行任务1,2,3
        startJob(job1);
        startJob(job2);
        startJob(job3);
    }

    public void startJob(JobInfo jobInfo) throws SchedulerException {
        quartzUtil.addTaskAndStart(jobInfo.getTriggerName(), jobInfo.getJobName(), jobInfo.getCron(), jobInfo.getJobData());
    }

}
